
from xml.sax import handler, make_parser

class TextReaderImpl(handler.ContentHandler):
    def __init__(self, controller):
        self.controller = controller
        self.tid        = None
        self.text       = None

    def startElement(self, name, attr):
        if name == "text":
            if attr.has_key("id"):
                self.tid = attr["id"]

    def characters(self, text):
        self.text = text

    def endElement(self, name):
        self.controller.addText(self.tid, self.text)
        self.tid  = None
        self.text = None

    def parse(self, pathAndFileName):
        parser = make_parser()
        parser.setContentHandler(self)
        parser.parse(pathAndFileName)
